var WxParse = require('../../wxParse/wxParse.js');
//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    
  },
  onLoad: function () {
    var that = this;
    // var expense = wx.getStorageSync('payreadExpense');
    // that.setData({
    //   expenseInfo: expense
    // })
    // console.log(that.data.expenseInfo);
    app.setNavtitle("专属会员");
    that.requestExpenseinfo();
  },
  onShow:function(){
    var that = this;
    
  },
  requestExpenseinfo: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_information_card_list'
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            expenseInfo: res.data.data
          })
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // 单次阅读付费
  paySingleread: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var data = {
      map: 'applet_information_pay',
      id: id,
      type: type
    };
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.moneyPay(res.data.data);
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
          that.setData({
            expenseInfo: ''
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  moneyPay: function (params) {
    var that = this;
    wx.requestPayment({
      'appId': params.appId,
      'timeStamp': params.timeStamp,
      'nonceStr': params.nonceStr,
      'package': params.package,
      'signType': params.signType,
      'paySign': params.paySign,
      'success': function (res) {
        wx.showLoading({
          title: '加载中',
        })
        setTimeout(function () {
          wx.hideLoading();
          wx.showModal({
            title: '',
            content: '支付成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta:1
                })
              }
            }
          })
        }, 1000)
      },
      'fail': function (res) {
        console.log(res);
        wx.showModal({
          title: '',
          content: '支付失败',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    });
  },
})

